package com.delos.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.delos.rbac.entity.RolesDO;
import com.delos.rbac.mapper.RolesMapper;
import com.delos.rbac.service.IRoleService;
import com.delos.rbac.vo.RolesVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author zouyaowen
 * @date 2020-06-28
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RolesMapper, RolesDO> implements IRoleService {
    @Override
    public RolesVO getByRoleId(Long roleId) {
        RolesDO rolesDO = this.getById(roleId);
        if (rolesDO == null) {
            return null;
        }
        RolesVO rolesVO = new RolesVO();
        BeanUtils.copyProperties(rolesDO, rolesVO);
        return rolesVO;
    }

    @Override
    public List<RolesVO> getByRoleId(List<Long> roleIdList) {
        QueryWrapper<RolesDO> query = new QueryWrapper<>();
        query.in("id", roleIdList);
        List<RolesDO> list = this.list(query);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        ArrayList<RolesVO> rolesVOList = new ArrayList<>();
        for (RolesDO rolesDO : list) {
            RolesVO rolesVO = new RolesVO();
            BeanUtils.copyProperties(rolesDO, rolesVO);
            rolesVOList.add(rolesVO);
        }
        return rolesVOList;
    }
}
